package com.szty.lib_pen.connectPen

import android.Manifest
import android.graphics.Color
import android.os.Bundle
import com.alibaba.android.arouter.facade.annotation.Route
import com.sjh.baseui.helper.NavigatorHelper
import com.sjh.baseutils.dp
import com.sjh.baseutils.setBaseAdapter
import com.sjh.lib_app.baseui.BaseActivity
import com.sjh.lib_app.event.SwitchFragment
import com.sjh.lib_app.utils.color
import com.szty.lib_pen.R
import com.szty.lib_pen.oidbluetooth.PermissionUtils
import kotlinx.android.synthetic.main.sp_activity_fragment.*
import net.lucode.hackware.magicindicator.ViewPagerHelper
import org.greenrobot.eventbus.Subscribe

@Route(path = "/lib_pen/devConnect", name = "智能笔主页")
class DevConnectActivity : BaseActivity() {
    private val scanName = arrayListOf("第一步", "第二步")
    private val REQUEST_LOCATION_CODE = 124 //请求位置权限
    private val fragments by lazy {
        mutableListOf(
            ConnectPenStepOne(),
            ConnectPenStepTwo()
        )
    }
    override fun onCreateRootView() = R.layout.sp_activity_fragment

    override fun onInitView(savedInstanceState: Bundle?) {
        initBlePermission()
        changeStatusBarColor(Color.TRANSPARENT, false)
        vp_connect_pen1.setBaseAdapter(supportFragmentManager, fragments)
        initIndicator()
    }

    override fun initListener() {
        img_scan_left1.setOnClickListener {
            finish()
        }
    }
    private fun initBlePermission() {
        PermissionUtils.requestPermissions(this, arrayOf(
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.ACCESS_COARSE_LOCATION
        ),
            REQUEST_LOCATION_CODE,
            "5.0之后使用蓝牙需要位置权限",
            object : PermissionUtils.OnPermissionResult() {
                override fun granted(requestCode: Int) {
                    //权限通过
                }

                override fun denied(requestCode: Int) {
                    //权限拒绝

                }
            })
    }

    override fun initData() {
    }
    private fun initIndicator() {
        NavigatorHelper.getInstance(this)
            .setColor(color(R.color.white), color(R.color.white))
            .setTextSize(14, 12)
            .setSelectHand {
                vp_connect_pen1.currentItem = it
            }
            .setLineWidth(70.dp)
            .setLineHeight(3.dp)
            .setLinePagerColor(color(R.color.white))
            .setRoundRadius(2.dp)
            .build(scanName, mi_connect_pen1)
        ViewPagerHelper.bind(mi_connect_pen1, vp_connect_pen1)
    }


    @Subscribe
    fun switchFragment( switchFragment: SwitchFragment){
        vp_connect_pen1.currentItem = switchFragment.pageIndex
    }
}